package dao

import (
	"github.com/astaxie/beego/orm"
	"xiaoercaidan/publicResources/models"
)

func InserImage(image models.GoodsImage) (error) {

	o := orm.NewOrm()
	_, err := o.Insert(&image)

	if err == nil {
		return nil
	} else {
		return err
	}

}

func InserShopImage(image models.ShopImage) (error) {

	o := orm.NewOrm()
	_, err := o.Insert(&image)

	if err == nil {
		return nil
	} else {
		return err
	}

}

func QeryImage(params map[string]string) (*[]models.GoodsImage) {

	o := orm.NewOrm()

	var images []models.GoodsImage

	var qs orm.QuerySeter

	qs = o.QueryTable("goods_image")

	for k, v := range params {
		qs = qs.Filter(k + "__exact", v)
	}

	qs.All(&images)

	if (len(images) > 0) {
		return &images
	} else {
		return nil
	}

}

func QeryImageByGoodsId(goodsId int64) (*[]models.GoodsImage) {

	o := orm.NewOrm()

	var images []models.GoodsImage

	var qs orm.QuerySeter

	qs = o.QueryTable("goods_image")

	qs = qs.Filter("good_id__exact", goodsId)

	qs.All(&images)

	if (len(images) > 0) {
		return &images
	} else {
		return nil
	}

}

func QeryImageByShopId(shopId int64) (*[]models.ShopImage) {

	o := orm.NewOrm()

	var images []models.ShopImage

	var qs orm.QuerySeter

	qs = o.QueryTable("shop_image")

	qs = qs.Filter("shop_id__exact", shopId)

	qs.All(&images)

	if (len(images) > 0) {
		return &images
	} else {
		return nil
	}

}

func DeleteImages(ids string) (error) {

	o := orm.NewOrm()

	sql := "delete from goods_image where good_id in(" + ids + ")"

	_, err := o.Raw(sql).Exec()

	return err
}
func DeleteImagesByShopId(ids string) (error) {

	o := orm.NewOrm()

	sql := "delete from shop_image where good_id in(" + ids + ")"

	_, err := o.Raw(sql).Exec()

	return err
}
